package com.ipay.wechat.listener;


import com.ipay.wechat.service.MerchantInfoService;
import com.ipay.wechat.service.TradeRecordService;

import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SystemListener implements ServletContextListener {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemListener.class);
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        try {
        	logger.error("系统初始化access_token---开始");
        	WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
            TradeRecordService trService = (TradeRecordService) app.getBean("trService");
            trService.freshAccessToken();
            logger.error("系统初始化access_token---结束");
            MerchantInfoService wechatService = (MerchantInfoService) app.getBean("merchantInfoService");
            wechatService.kafkaConsumer();
        } catch (Throwable e) {
            logger.error("系统初始化失败!",e);
        }
    }
}